package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 新文化网
 * @author Persh
 *
 */
public class XwhNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://[a-z]*.xwh.cn/news/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://[a-z]*.xwh.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://[a-z]*.xwh.cn/article/.*/[0-9]*.html",
		"http://news.xwh.cn/[0-9\\/]+.shtml"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		String html=doc.select("meta[http-equiv=refresh]").toString();
		String tempUrl=StringUtils.substringBetween(html, "url=", "\"");
		if(StringUtils.isNotBlank(tempUrl)){
			if(!isDetailPage(tempUrl)){
				news.setUpdateUrl(ParseState.ERR404.toString());
				return news;
			}else{
				urlMeta=CrawlHTML.responseToURL(tempUrl);
				if (urlMeta.getHtml() == null) {
				}	
				htmltxt=urlMeta.getHtml();
				doc=Jsoup.parse(htmltxt);
			}
		}
		
		title =doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("p.ygbignews01").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#zmcon-info"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#news_content"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.a_con"));
			}
		}
		date=DateUtils.matchDate(doc.select("div.zmcon-tipl").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("p.ygbignews02").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.a_info").text());
			}
		}

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("div.zmcon-tipl").text();
		author=StringUtils.substringBetween(authorStr, "来源:"," ");
		if(StringUtils.isBlank(author)){
			author=StringUtils.substringAfter(authorStr, "来源:");
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		XwhNewsAnalyse analyse=new XwhNewsAnalyse();
		String url="http://news.xwh.cn/2015/1210/353790.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
